﻿P.Utl.Pager = function (div) {
    div = $(div);
    var html = '<div dom="pageList" class="g-jp-wrap g-jp-color">总计<span dom="recordCount"></span>条，每页显示<span dom="pageSize"></span>条 <a alt="转到第一页" title="转到第一页" dom="firstPage" href="#" page="1">&lt;&lt;</a><a alt="上一页" title="上一页" dom="prevPage" href="#" page="1">&lt;</a><em>第</em><select dom="currentPage"><option value="1">1</option></select><em>页</em><em class="totalPage">共<span dom="totalPage">0</span>页</em><a alt="下一页" title="下一页" dom="nextPage" href="#" page="1">&gt;</a><a alt="转到最后一页" title="转到最后一页" dom="lastPage" href="#" page="1">&gt;&gt;</a></div>';
    div.html(html);
    this.els = $.UT.DomSelector($("a,span,select", div));
    $(div).bind("click", function (e) {
        var page = parseInt(e.target.getAttribute('page'));
        if (page) {
            div.triggerHandler('pageChange', {page:page, target:e.target});
        }
        if (e.target.nodeName == "A") {
            return false;
        }
    });
    $(div).bind("change", function (e) {
        var page = parseInt(e.target.value);
        if (page) {
            div.triggerHandler('pageChange', { page: page, target: e.target });
        }
        return false;
    });
};
P.Utl.Pager.prototype.setPage = function (totalPage, curPage, recordCount, pageSize) {
    totalPage = parseInt(totalPage);
    curPage = parseInt(curPage);
    var prev = curPage - 1,
        next = curPage + 1,
        j, opt, sel = this.els["currentPage"][0];
    this.els["totalPage"][0].innerHTML = totalPage;
    this.els["recordCount"][0].innerHTML = recordCount;
    this.els["pageSize"][0].innerHTML = pageSize;
    this.els["currentPage"][0].options.length = 0;
    if (sel.options.length != totalPage) {
        sel.options.length = 0;
        for (j = 1; j <= totalPage; j++) {
            opt = new Option(j, j);
            if (j == curPage) { opt.selected = true; }
            sel.options.add(opt);
        }
        this.els["firstPage"][0].setAttribute("page", "1");
        this.els["lastPage"][0].setAttribute("page", totalPage);
    }

    this.els["prevPage"][0].setAttribute("page", (prev < 1) ? 1 : prev);
    this.els["nextPage"][0].setAttribute("page", (next > totalPage) ? totalPage : next);
};
P.Utl.DateRangePicker = function (dfrom, dto) {
    dfrom = $(dfrom);
    dto = $(dto);
    P.Utl.DatePicker(dfrom);
    P.Utl.DatePicker(dto);
    dfrom.datepicker("option", "onSelect", function (selectedDate) {
        var date = selectedDate;
        dto.datepicker("option", "minDate", date);
    });
    dto.datepicker("option", "onSelect", function (selectedDate) {
        var date = selectedDate;
        dfrom.not(this).datepicker("option", "maxDate", date);
    });
};

P.Utl.DatePicker = function (input) {
    var opt = {
        dateFormat: 'yy-mm-dd',
        //maxDate: "+0D",
        dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
        monthNames: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
    };

    $(input).attr("readonly", "readonly");
    $(input).datepicker(opt).bind("blur", function (e) {
        var newDate, yy, mm, dd;
        if (this.value) {
            if (!(new RegExp("^\\d{4}-\\d{1,2}-\\d{1,2}$").test(this.value))) {
                $.UT.Alert("请输入正确的日期格式 例:yyyy-mm-dd");
                this.value = "";
            } else {
                newDate = new RegExp("^(\\d{4})-(\\d{1,2})-(\\d{1,2})$").exec(this.value);
                yy = newDate[1];
                mm = newDate[2];
                dd = newDate[3];
                newDate = new Date(mm + "/" + dd + "/" + yy);
                if (!newDate) {
                    $.UT.Alert("请输入正确的日期格式 例:yyyy-mm-dd");
                    this.value = "";
                } else {
                    yy = newDate.getFullYear();
                    mm = (newDate.getMonth() + 1).toString();
                    dd = (newDate.getDate()).toString();
                    if (mm.length < 2) {
                        mm = "0" + mm;
                    }
                    if (dd.length < 2) {
                        dd = "0" + dd;
                    }
                    newDate = yy + "-" + mm + "-" + dd;
                    this.value = newDate;
                }
            }
        }
    });

    $(input).bind("keyup", function (e) {
        if (e.keyCode === 8 || e.keyCode === 13) {
            return;
        }
    });
};


